<?php

namespace Wechat\Controller;

use Org\Net\IpLocation;
use Think\Controller;

class ScienceController extends CommonController {

    /**
     * 全部疾病列表页
     */
    public function index() {
        $this->display("index");
    }
    
    /*
     * 科普疾病列表
     */
    public function scienceList() {

        $categoryId = I("post.categoryId");

        if(!isNullOrEmpty($categoryId)) {
            $categoryMap["category_id"] = $categoryId;
        }
        $categoryMap["c.show"] = 1;
        $categoryList = D("Category")->alias("c")->where($categoryMap)->select();

        foreach ($categoryList as &$category) {

            $itemMap["category_id"] = $category["category_id"];
            $category["science_list"] = D("Science")
                ->where($itemMap)
                ->select();

            $activityMap["a.category_id"] = $category["category_id"];
            $activityMap["a.status"] = array("in", array(
                C("ACTIVITY_STATUS.WAITING_JOIN"),
                C("ACTIVITY_STATUS.FULL")
            ));
            $activityList = D("Activity")->alias("a")
                ->where($activityMap)
                ->field("a.activity_id")
                ->select();

            $category["has_activity"] = !isNullOrEmpty($activityList);

        }

        echo json_encode($categoryList);
    }

    /**
     * 科普详情页
     */
    public function detail() {

        $id = $_REQUEST["id"];
        $info = D("Science")->queryById($id);

        $info["read_count"] = $info["read_count"] + 1;
        D("Science")->updateInfo($info);

        $this->assign("customer", $this->customer);
        $this->assign("info", $info);
        $this->display("detail");

    }

}